using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CheckInBook : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnBarcode_Click(object sender, EventArgs e)
    {
        try
        {
            int rowsAffected_mcoBook;
            Boolean bookExist, coBookExist, coBookReturnedExist;
            lblErrorMsg.Text = "";

            ManageCheckedOutBook mcoBook = new ManageCheckedOutBook();
            ManageBook mBook = new ManageBook();

            bookExist = mBook.bookExists(txtBarcode.Text);
            if (bookExist)//if book exists in book table
            {
                coBookExist = mcoBook.checkedOutBookExists(txtBarcode.Text);
                coBookReturnedExist = mcoBook.checkedOutBookReturnedExists(txtBarcode.Text);
                if (coBookExist)//if book exists in checkedOutBook table with status of checkedOut
                {
                    rowsAffected_mcoBook = mcoBook.checkInBook(txtBarcode.Text);
                    if (rowsAffected_mcoBook > 0)
                    {
                        lblErrorMsg.Text = "The Barcode " + txtBarcode.Text + " successfully checked in.";
                    }
                    else
                    {
                        lblErrorMsg.Text = "The Barcode " + txtBarcode.Text + " has already been checked in.";
                    }
                }
                else if (coBookReturnedExist)//book is in checkedOutBook table with status of returned
                {
                    lblErrorMsg.Text = "The Barcode " + txtBarcode.Text + " has already been checked in.";
                }
                else//book is not in checkedOutBook table
                {
                    lblErrorMsg.Text = "The Barcode " + txtBarcode.Text + " has not been checked out.";
                }
            }
            else
            {
                lblErrorMsg.Text = "The Barcode " + txtBarcode.Text + " does not exist in the system.";
            }

            //GridView_CheckedInBooks.DataBind();
        }
        catch (Exception ex)
        {
            if (ex.Message != "")
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
    }
}
